/*
 * ConnectionInfo.cpp
 *
 *  Created on: 09.12.2012
 *      Author: zheka
 */

#include "ConnectionInfo.h"
#include "util/ProxyException.hpp"
sockaddr * ConnectionInfo::getAddress() {
	return (sockaddr*) &address;
}
size_t ConnectionInfo::getAddressSize() {
	return sizeof(address);
}
int ConnectionInfo::getPort() {
	return port;
}

std::string ConnectionInfo::getHost() {
	return hostName;
}
std::string ConnectionInfo::getHostAddress() {
	return hostAddress;
}
void ConnectionInfo::resolveHost() {
	hostent * host;
	if ((host = gethostbyname(hostName.c_str())) == NULL) {
		throw ProxyException(
				"[NetworkUtils] Unable resolve host [" + hostName + "]");
	}

	address.sin_addr.s_addr = *((unsigned long *) host->h_addr_list[0]);
	hostAddress = std::string(inet_ntoa(*(struct in_addr *) host->h_addr));
}
ConnectionInfo::ConnectionInfo(std::string hostName, int port) :
		hostName(hostName), port(port) {

	address.sin_family = AF_INET;

	address.sin_port = htons(port);

}
